package com.wbase.base.utils

import android.app.ActivityManager
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Process
import java.io.IOException
import java.util.*
import java.util.zip.ZipEntry
import java.util.zip.ZipFile

object PackageUtils {
    /**
     * 得到Application的指定的KEY的MetaData信息
     */
    fun getApplicationMetadata(
        context: Context,
        metaDataKey: String?): String? {
        var info: ApplicationInfo? = null
        try {
            val pm = context.packageManager
            info = pm.getApplicationInfo(context.packageName, PackageManager.GET_META_DATA)
            val value = info.metaData[metaDataKey]
            if (value != null) {
                return value.toString()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    /**
     * getPackageName:得到应用包名. <br></br>
     *
     * @return
     * @author wangheng
     */
    fun getPackageName(context: Context): String? {
        return context.packageName
    }

    /**
     * getVersionName:得到版本名称. <br></br>
     *
     * @return
     * @author wangheng
     */
    fun getVersionName(context: Context): String? {
        try {
            val manager = context.packageManager
            val info = manager.getPackageInfo(context.packageName, 0)
            return info.versionName
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return "1.0"
    }

    fun getApplicationInfo(context: Context): ApplicationInfo? {
        return context.applicationInfo
    }

    /**
     * getVersionCode:得到版本号. <br></br>
     *
     * @return
     * @author wangheng
     */
    fun getVersionCode(context: Context): Int {
        try {
            val manager = context.packageManager
            val info = manager.getPackageInfo(context.packageName, 0)
            return info.versionCode
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 1
    }

    /**
     * getMetaInfChannel:得到APK下的META-INF目录下的渠道号. <br></br>
     *
     * @param context context
     * @return 渠道号
     * @author wangheng
     */
    fun getMetaInfChannel(context: Context): String? {
        val appinfo = context.applicationInfo
        val sourceDir = appinfo.sourceDir
        var ret = ""
        var zipfile: ZipFile? = null
        try {
            zipfile = ZipFile(sourceDir)
            val entries: Enumeration<*> = zipfile.entries()
            while (entries.hasMoreElements()) {
                val entry =
                    entries.nextElement() as ZipEntry
                val entryName = entry.name
                //如果想修改此标示，直接编辑package.py即可
                if (entryName.startsWith("META-INF/reshow")) {
                    ret = entryName
                    break
                }
            }
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            if (zipfile != null) {
                try {
                    zipfile.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
        val split = ret.split("_").toTypedArray()
        return if (split.size >= 2) {
            ret.substring(split[0].length + 1)
        } else {
            ""
        }
    }

    fun isAppProcess(context: Context): Boolean {
        try {
            val pid = Process.myPid()
            val packageName = getPackageName(context)
            val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            val list = am.runningAppProcesses
            if (list != null) {
                for (info in list) {
                    if (pid == info.pid && packageName == info.processName) {
                        return true
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }
}